package com.zjp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjp.pojo.*;
import com.zjp.service.AreaService;
import com.zjp.service.InoculationSiteService;
import com.zjp.vo.I_SiteAndArea;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

/**
 * 接种点管理模块
 */
//接种点管理
@RestController
@Slf4j
public class InoculationSiteController {

    @Autowired
    private InoculationSiteService iSiteService;

    @Autowired
    private AreaService areaService;

    /**
     * 查询所有接种点
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAllISite")   //查找所有接种点
    public ModelAndView findAllISite(@RequestParam(name = "pageNo",required = false,defaultValue = "1") int pageNo,
                                     @RequestParam(name = "pageSize",required = false,defaultValue = "5") int pageSize){
        ModelAndView mv=new ModelAndView();
        PageHelper.startPage(pageNo,pageSize);   //开启分页
        List<I_SiteAndArea> siteList=iSiteService.findAllISite();
        log.info("查询所有接种点：{}",siteList);
        mv.addObject("siteList",siteList);
        PageInfo pageInfo=new PageInfo<>(siteList);  //存储分页信息
        mv.addObject("pageInfo",pageInfo);
        List<Province> provinceList=areaService.findAllProvince();
        mv.addObject("provinceList",provinceList);
        List<City> cityList=areaService.findAllCity();
        mv.addObject("cityList",cityList);
        List<Area> areaList=areaService.findAllArea();
        mv.addObject("areaList",areaList);
        mv.setViewName("inoculationSite");
        return mv;
    }

    /**
     * 添加方法实现新增接种点功能-接种点-添加接种点
     */
    @RequestMapping("/addInoculationSite")
    public String addInoculationSite(InoculationSite inoculationSite) {
        int i = iSiteService.addInoculationSite(inoculationSite);
        log.info("添加接种点信息：{}", inoculationSite);
        if (i > 0) {
            return "SUCCESS";
        }
        return "error";
    }

    /**
     * 接种点管理中增加添加接种疫苗方法。
     */
    @RequestMapping("/addIvaccine")
    public String addIvaccine(IVaccines iVaccines){
        int i=iSiteService.addIvaccine(iVaccines);
        if(i>0){
            return "SUCCESS";
        }
        return "error";
    }

    /**
     * 修改接种点
     */
    @RequestMapping("/updateInoculationSite")
    public String updateInoculationSite(InoculationSite inoculationSite) {
        int i = iSiteService.updateInoculationSite(inoculationSite);
        log.info("修改接种点信息：{}", inoculationSite);
        if (i > 0) {
            return "SUCCESS";
        }
        return "error";
    }

    /**
     * 删除接种点
     */
    @RequestMapping("/deleteByInoculationid")
    public String deleteByInoculationid(Long inoculationid) {
        int i = iSiteService.deleteByInoculationid(inoculationid);
        log.info("删除接种点信息：{}", inoculationid);
        if (i > 0) {
            return "SUSSES";
        }
        return "FAIL";
    }

}
